package com.orangelife.mobile.common.fragment;

import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.common.jump.CommonJumper;
import com.curry.android.R;
import com.curry.android.net.NetworkUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.datatrack.DataTrack;
import com.orangelife.mobile.login.activity.Login2Activity;
import com.umeng.analytics.MobclickAgent;

import java.util.Map;

public class OrangeLifeBaseFragment extends Fragment {

    protected Dialog dialog = null;

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
        dataTrackBrowse();
    }

    /** 缓存的viewiew需要判断是否已经被加过parent，如果有parent需要从parent删除，要不然会发生这个view已经有parent的错误。 */
    protected void clearChildView(View childView) {
        ViewGroup parent = (ViewGroup) childView.getParent();
        if (parent != null) {
            parent.removeView(childView);
        }
    }

    public Map<String,Object> getDataTrack(){
        return null;
    };

    /**
     * 诸葛全局浏览采集，在将来要改成抽象方法
     */
    private void dataTrackBrowse(){
        DataTrack.browseTrack(getDataTrack());
    }

    public void dataTrackClick(String ui_elmt,Map<String,Object> map){
        DataTrack.clickTrack(ui_elmt,map);
    }
    /**
     * 是否联网
     *
     * @param isToast 是否弹出Toast提示
     * @return true-联网，false-断网；
     */
    protected boolean isConnected(boolean isToast) {
        if (!NetworkUtil.isConnectedNet(getActivity())) {
            if (isToast) {
                Toast.makeText(getActivity(),
                        getResources().getString(R.string.net_disconnected),
                        Toast.LENGTH_SHORT).show();
            }
            return false;
        }
        return true;
    }

    protected boolean isConnected() {
        return isConnected(true);
    }

    /**
     * 需要重新登录app
     *
     * @param dialog 弹出框
     */
    protected void isLogin(Dialog dialog) {
        DialogHelper.closeDialog(dialog);
        CommonJumper.jumpToLogin(getActivity());
    }

    protected void showDialog() {
        if (getActivity() != null) {
            dialog = DialogHelper.getInstance().createLoadingDialog(
                    getActivity());
            dialog.show();
        }
    }

    protected void closeDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
